<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java容器类线程安全性解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f9f9f9;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #3a7bd5 0%, #00d2ff 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .thread-safe {
            border-left: 4px solid #10b981;
        }
        .non-thread-safe {
            border-left: 4px solid #ef4444;
        }
        .feature-icon {
            width: 48px;
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            margin-bottom: 1rem;
        }
        .safe-icon {
            background-color: #d1fae5;
            color: #10b981;
        }
        .unsafe-icon {
            background-color: #fee2e2;
            color: #ef4444;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-4">Java容器类线程安全性解析</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-3xl">深入理解多线程环境下的数据结构选择</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#Java</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#多线程</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#数据结构</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="prose max-w-none">
                <p class="text-lg text-gray-700 mb-6">在 Java 中，容器类是用于存储和管理数据的核心数据结构，它们可以分为线程安全和非线程安全两类。线程安全的容器在多线程环境中可以安全地被多个线程同时访问和操作，而非线程安全的容器在多线程环境中需要额外的同步措施以确保线程安全。</p>
            </div>
        </section>

        <!-- Comparison Table -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">线程安全与不安全容器对比</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-8 p-6">
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="feature-icon safe-icon">
                                <i class="fas fa-lock text-2xl"></i>
                            </div>
                            <h3 class="text-2xl font-semibold ml-3">线程安全容器</h3>
                        </div>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>内置同步机制，可直接用于多线程环境</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>通常性能略低但更安全</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>无需额外同步代码</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="feature-icon unsafe-icon">
                                <i class="fas fa-unlock text-2xl"></i>
                            </div>
                            <h3 class="text-2xl font-semibold ml-3">非线程安全容器</h3>
                        </div>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>没有内置同步机制</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>单线程环境下性能更高</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-triangle text-yellow-500 mt-1 mr-2"></i>
                                <span>多线程环境下需要额外同步</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Thread Safe Containers -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">线程安全容器</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Vector -->
                <div class="card bg-white rounded-lg overflow-hidden thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-shield-alt text-green-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">Vector</h3>
                        </div>
                        <p class="text-gray-700 mb-4">一个线程安全的动态数组实现，它的所有方法都被同步。虽然它是线程安全的，但由于每个方法都有同步操作，因此性能上可能会有一定的开销。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">同步</span>
                            <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">数组</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.0</span>
                        </div>
                    </div>
                </div>

                <!-- Hashtable -->
                <div class="card bg-white rounded-lg overflow-hidden thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-shield-alt text-green-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">Hashtable</h3>
                        </div>
                        <p class="text-gray-700 mb-4">一个线程安全的哈希表实现，它的所有方法都被同步。与 HashMap 不同，Hashtable 是同步的，因此可以在多线程环境中安全地使用。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">同步</span>
                            <span class="px-3 py-1 bg-purple-100 text-purple-800 text-sm rounded-full">哈希表</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.0</span>
                        </div>
                    </div>
                </div>

                <!-- ConcurrentHashMap -->
                <div class="card bg-white rounded-lg overflow-hidden thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-shield-alt text-green-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">ConcurrentHashMap</h3>
                        </div>
                        <p class="text-gray-700 mb-4">一个高性能的线程安全哈希表实现，它通过分段锁机制来实现高并发的安全性。相比 Hashtable，ConcurrentHashMap 提供了更高的性能和可扩展性。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">分段锁</span>
                            <span class="px-3 py-1 bg-purple-100 text-purple-800 text-sm rounded-full">哈希表</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.5</span>
                        </div>
                    </div>
                </div>

                <!-- CopyOnWriteArrayList -->
                <div class="card bg-white rounded-lg overflow-hidden thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-shield-alt text-green-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">CopyOnWriteArrayList</h3>
                        </div>
                        <p class="text-gray-700 mb-4">一个线程安全的变体的 ArrayList，它在写操作时会复制数组，适用于读多写少的场景。它确保了在多线程环境中的安全性，但可能会有较高的内存和性能开销。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">写时复制</span>
                            <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">数组</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.5</span>
                        </div>
                    </div>
                </div>

                <!-- BlockingQueue -->
                <div class="card bg-white rounded-lg overflow-hidden thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-shield-alt text-green-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">BlockingQueue及其实现</h3>
                        </div>
                        <p class="text-gray-700 mb-4">如 LinkedBlockingQueue、ArrayBlockingQueue 和 PriorityBlockingQueue，这些实现是线程安全的，通常用于生产者-消费者模式等需要线程安全队列的场景。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">阻塞队列</span>
                            <span class="px-3 py-1 bg-indigo-100 text-indigo-800 text-sm rounded-full">生产者-消费者</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.5</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Non-Thread Safe Containers -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">非线程安全容器</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- ArrayList -->
                <div class="card bg-white rounded-lg overflow-hidden non-thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-exclamation-triangle text-red-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">ArrayList</h3>
                        </div>
                        <p class="text-gray-700 mb-4">是一个非线程安全的动态数组实现。如果在多线程环境中使用 ArrayList，需要显式地同步它的访问。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-red-100 text-red-800 text-sm rounded-full">非同步</span>
                            <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">数组</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.2</span>
                        </div>
                    </div>
                </div>

                <!-- HashMap -->
                <div class="card bg-white rounded-lg overflow-hidden non-thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-exclamation-triangle text-red-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">HashMap</h3>
                        </div>
                        <p class="text-gray-700 mb-4">是一个非线程安全的哈希表实现。在多线程环境中，如果多个线程同时访问和修改 HashMap，可能会导致数据不一致和其他并发问题。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-red-100 text-red-800 text-sm rounded-full">非同步</span>
                            <span class="px-3 py-1 bg-purple-100 text-purple-800 text-sm rounded-full">哈希表</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.2</span>
                        </div>
                    </div>
                </div>

                <!-- LinkedList -->
                <div class="card bg-white rounded-lg overflow-hidden non-thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-exclamation-triangle text-red-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">LinkedList</h3>
                        </div>
                        <p class="text-gray-700 mb-4">是一个非线程安全的链表实现。它支持双向链表结构，但在多线程环境中需要额外的同步措施来保证线程安全。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-red-100 text-red-800 text-sm rounded-full">非同步</span>
                            <span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">链表</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.2</span>
                        </div>
                    </div>
                </div>

                <!-- TreeMap -->
                <div class="card bg-white rounded-lg overflow-hidden non-thread-safe">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-exclamation-triangle text-red-500 mr-2"></i>
                            <h3 class="text-xl font-semibold">TreeMap</h3>
                        </div>
                        <p class="text-gray-700 mb-4">是一个非线程安全的红黑树实现，提供了有序的键值对存储。如果在多线程环境中使用 TreeMap，需要进行显式的同步。</p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-red-100 text-red-800 text-sm rounded-full">非同步</span>
                            <span class="px-3 py-1 bg-green-100 text-green-800 text-sm rounded-full">红黑树</span>
                            <span class="px-3 py-1 bg-gray-100 text-gray-800 text-sm rounded-full">JDK1.2</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">容器类关系图</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6">
                <div class="mermaid">
                    graph TD
                    A[容器接口] --> B[Collection]
                    A --> C[Map]
                    B --> D[List]
                    B --> E[Set]
                    B --> F[Queue]
                    D --> G[ArrayList]
                    D --> H[LinkedList]
                    D --> I[Vector]
                    D --> J[CopyOnWriteArrayList]
                    E --> K[HashSet]
                    E --> L[TreeSet]
                    E --> M[CopyOnWriteArraySet]
                    F --> N[BlockingQueue]
                    F --> O[PriorityQueue]
                    C --> P[HashMap]
                    C --> Q[TreeMap]
                    C --> R[Hashtable]
                    C --> S[ConcurrentHashMap]
                    C --> T[ConcurrentSkipListMap]
                    
                    style I fill:#d1fae5,stroke:#10b981
                    style J fill:#d1fae5,stroke:#10b981
                    style M fill:#d1fae5,stroke:#10b981
                    style N fill:#d1fae5,stroke:#10b981
                    style R fill:#d1fae5,stroke:#10b981
                    style S fill:#d1fae5,stroke:#10b981
                    style T fill:#d1fae5,stroke:#10b981
                    
                    style G fill:#fee2e2,stroke:#ef4444
                    style H fill:#fee2e2,stroke:#ef4444
                    style K fill:#fee2e2,stroke:#ef4444
                    style L fill:#fee2e2,stroke:#ef4444
                    style O fill:#fee2e2,stroke:#ef4444
                    style P fill:#fee2e2,stroke:#ef4444
                    style Q fill:#fee2e2,stroke:#ef4444
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">选择建议</h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-lightbulb text-yellow-500 text-2xl mr-3"></i>
                        <h3 class="text-xl font-semibold">单线程环境</h3>
                    </div>
                    <p class="text-gray-700">优先选择非线程安全容器，如ArrayList、HashMap等，它们性能更高且没有不必要的同步开销。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-users text-blue-500 text-2xl mr-3"></i>
                        <h3 class="text-xl font-semibold">读多写少场景</h3>
                    </div>
                    <p class="text-gray-700">考虑使用CopyOnWriteArrayList或CopyOnWriteArraySet，它们牺牲写性能换取更高的读性能。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-cogs text-green-500 text-2xl mr-3"></i>
                        <h3 class="text-xl font-semibold">高并发场景</h3>
                    </div>
                    <p class="text-gray-700">优先选择ConcurrentHashMap、ConcurrentSkipListMap等现代并发容器，它们提供更好的并发性能。</p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center">
                <h3 class="text-xl font-semibold mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition-colors duration-200">
                    <i class="fas fa-link mr-1"></i> www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>